package org.ldx.lotterysystem.service.impl;

import jakarta.annotation.Resource;
import org.ldx.lotterysystem.controller.param.CreatePrizeParam;
import org.ldx.lotterysystem.controller.param.PageParam;
import org.ldx.lotterysystem.dao.dataobject.PrizeDo;
import org.ldx.lotterysystem.dao.mapper.PrizeMapper;
import org.ldx.lotterysystem.service.PictureService;
import org.ldx.lotterysystem.service.PrizeService;
import org.ldx.lotterysystem.service.dto.PrizeDTO;
import org.ldx.lotterysystem.service.dto.PageListDTO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {
    @Resource
    PrizeMapper prizeMapper;
    @Resource
    PictureService pictureService;
    @Override
    public long createPrize(CreatePrizeParam param, MultipartFile file) {
        //上传图片
        String fileName = pictureService.savePicture(file);
        //存储到数据库
        PrizeDo prizeDo = new PrizeDo();
        prizeDo.setDescription(param.getDescription());
        prizeDo.setName(param.getPrizeName());
        prizeDo.setPrice(param.getPrice());
        prizeDo.setImageUrl(fileName);
        prizeMapper.insert(prizeDo);
        return prizeDo.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        //获取总量
        int total = prizeMapper.getPrizeCount();
        //查询商品列表
        List<PrizeDTO> prizeDTOList = new ArrayList<>();
        List<PrizeDo> prizeDOList = prizeMapper.getPrizeList(param.offset(), param.getPageSize());
        for (PrizeDo prizeDo : prizeDOList) {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setDescription(prizeDo.getDescription());
            prizeDTO.setPrizeId(prizeDo.getId());
            prizeDTO.setName(prizeDo.getName());
            prizeDTO.setPrice(prizeDo.getPrice());
            prizeDTO.setImageUrl(prizeDo.getImageUrl());
            prizeDTOList.add(prizeDTO);
        }
        PageListDTO<PrizeDTO> result = new PageListDTO<>(total, prizeDTOList);
        return result;
    }
}
